package aliasMatching.controller;

import aliasMatching.model.DisplayInfo;
import aliasMatching.model.MyTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import rdfparser.db.DBConnection;

/**
 *
 * This class is responsible for finding the threads equality.
 * It will check whether our test user and rest of the users have replied in the
 * same thread or not.
 * @author amendrashrestha
 */
public final class ThreadEquality {

    List userList;
    List username;
    public static List secondUserThreadList;
    public static List firstUserThreadList;

    public ThreadEquality(List userIDList, List user, MyTableModel tableMdl) throws SQLException {
        this.userList = userIDList;
        this.username = user;
        List totalCommonThreadList = new ArrayList();

        for (int i = 1; i < userList.size(); i++) {
            //for(int j = 1+i; j < userList.size(); j++){
                totalCommonThreadList = getCommonThread(Integer.parseInt(userList.get(0).toString()),
                        Integer.parseInt(userList.get(i).toString())
                        );

                String common = "";
                List temp = (List) totalCommonThreadList;

                if (temp.isEmpty()) {
                    common = "0";
                } else {
                    for (Object k : temp) {
                        if (common.length() > 0) {
                            common += ",";
                        }
                        common += k;
                    }
                }
                int totalCommonThreadCount = temp.size();
                float equalPercentage;
                int selectedNodeThreads = firstUserThreadList.size() + secondUserThreadList.size() + 1;
                //System.out.println(selectedNodeFriends);
                equalPercentage = ((totalCommonThreadCount * 100) / selectedNodeThreads);

                DisplayInfo di = new DisplayInfo(username.get(0).toString(),
                        userList.get(i).toString(),
                        username.get(i).toString(),
                        common, equalPercentage, "Thread");
                tableMdl.SetValue(di);
          //  }
        }
    }

    /**
     * Returns the list of friends to compare with our test user.
     */
    public List getAllThreads(int ID) throws SQLException {
        List threads = new ArrayList();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = null;
        //System.out.println("ID:" + ID);

        con = DBConnection.getDBConnection();
        String querypostID = "select distinct postTable.MakerID, "
                + "postTable.PostID, ThreadID "
                + "from `tbl_threads_container` threadTable,"
                + "(select MakerID, PostID from tbl_posts "
                //+ "where MakerID = '59529') postTable "
                + "where MakerID = \'" + ID + "\') postTable "
                + "where postTable.PostID = threadTable.PostID";
        //System.out.println(querypostID);

        try {
            ps = con.prepareStatement(querypostID);
            rs = ps.executeQuery();
            while (rs.next()) {
                threads.add(rs.getInt(3));
                //System.out.println("getAllFrindsList: " +rs.getInt(1));
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return threads;
    }

    /**
     * returns common friends between our test user and other user
     */
    public List getCommonThread(int firstID, int secondID) throws SQLException {
        //System.out.println("getCommongFrildaskdfj: " + compareID);
        //ArrayList testUserList = testUserFriendList;
        firstUserThreadList = getAllThreads(firstID);
        secondUserThreadList = getAllThreads(secondID);
        List commonThreadList = new ArrayList();

        for (Object firstList : firstUserThreadList) {
            //System.out.print("X="+firstList + "\n");
            int first = Integer.parseInt(firstList.toString());
            for (Object secondList : secondUserThreadList) {
                //System.out.println("j="+secondList);
                int second = Integer.parseInt(secondList.toString());
                if (first == second) {
                    //System.out.println("Equal" + first);
                    commonThreadList.add(first);
                    break;
                }
            }
        }
        return commonThreadList;
    }
}
